home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / PublicDomain / Anwendungen / LimitWhite / Source / LimitWhite.C < prev    next >
C/C++ Source or Header  |  2000-03-16  |  6KB  |  222 lines

  1. /* ------------------------------------------------------------------------------------------------------------ */
  2. /*                                                                                                              *
  3.  *   LimitWhite V1.0 Main Module
  4.  *                                                                                                              */
  5. /* ------------------------------------------------------------------------------------------------------------ */
  6.  
  7. #include "LimitWhite.h"
  8. #include "Peripherel.C"
  9. #include "globals.h"
  10. #include "protos.h"
  11.  
  12. /* ------------------------------------------------------------------------------------------------------------ */
  13.  
  14. int main(int ac, char *av[])
  15. {
  16.     CheckOSVer();                                                               /* Check for OS3.0+             */
  17.     InstallPatches();                                                           /* Patch the OS functions       */
  18.  
  19.     DoIDCMP();                                                                  /* Enter IDCMP loop             */
  20.  
  21.     RemovePatches();                                                            /* Attempt to remove OS patches */
  22.  
  23.     CleanUp();                                                                  /* Free system resources        */
  24. } /* if */
  25.  
  26. /* ------------------------------------------------------------------------------------------------------------ */
  27.  
  28. void wbmain(struct WBStartup *wbmsg)
  29. {
  30.     /* Program started from WB. Pass control over to main() */
  31.  
  32.     main(0, NULL);
  33. } /* wbmain() */
  34.  
  35. /* ------------------------------------------------------------------------------------------------------------ */
  36.  
  37. void CheckOSVer()
  38. {
  39.     struct Library *VersionBase;
  40.  
  41.     /* Check OS version is at least V39 (OS 3.0) */
  42.  
  43.     if (VersionBase = OpenLibrary("version.library", 0))
  44.     {
  45.         if (VersionBase->lib_Version < 39)
  46.         {
  47.             /* OS is 2.1 or lower */
  48.  
  49.             DoEasyReq("LimitWhite V1.0 requires at least OS3.0");
  50.  
  51.             CloseLibrary(VersionBase);
  52.             CleanUp();
  53.         } /* if */
  54.  
  55.         CloseLibrary(VersionBase);
  56.     } /* if */
  57. } /* CheckOSVer() */
  58.  
  59. /* ------------------------------------------------------------------------------------------------------------ */
  60.  
  61. void DoIDCMP()
  62. {
  63.     CxMsg *msg;
  64.     ULONG sigs = 0, msgid, msgtype;
  65.  
  66.     /* IDCMP loop */
  67.  
  68.     /* First, create and activate the commodity */
  69.  
  70.     SetupCx();
  71.  
  72.     /* Enter IDCMP loop */
  73.  
  74.     do
  75.     {
  76.         /* Wait for Cx and Ctrl-C signals */
  77.  
  78.         sigs = Wait((1L << Cx_Broker_Mp->mp_SigBit) | SIGBREAKF_CTRL_C);
  79.  
  80.         if (sigs & SIGBREAKF_CTRL_C)
  81.         {
  82.             AttemptExit();
  83.         } /* if */
  84.  
  85.         else
  86.         {
  87.             /* Cx message */
  88.  
  89.             while (msg = (CxMsg *)GetMsg(Cx_Broker_Mp))
  90.             {
  91.                 /* Get message details and reply to it */
  92.  
  93.                 msgid = CxMsgID(msg);
  94.                 msgtype = CxMsgType(msg);
  95.  
  96.                 ReplyMsg((struct Message *)msg);
  97.  
  98.                 /* Act upon the message */
  99.  
  100.                 switch(msgtype)
  101.                 {
  102.                     case CXM_COMMAND:
  103.  
  104.                         /* Commodities command */
  105.  
  106.                         switch(msgid)
  107.                         {
  108.                             case CXCMD_KILL:
  109.  
  110.                                 /* Attempt to exit program */
  111.  
  112.                                 AttemptExit();
  113.  
  114.                                 break;
  115.  
  116.                             case CXCMD_UNIQUE:
  117.  
  118.                                 /* User attempted to start another copy of LimitWhite */
  119.  
  120.                                 DoEasyReq("LimitWhite is already running");
  121.  
  122.                                 break;
  123.  
  124.                             default:
  125.  
  126.                                 /* Show/Hide interface/Enable/Disable. Not relevant */
  127.  
  128.                                 break;
  129.                         } /* switch */
  130.  
  131.                         break;
  132.  
  133.                     default:
  134.  
  135.                         /* Show/Hide interface. Not relevant */
  136.  
  137.                         break;
  138.                 } /* switch */
  139.             } /* while */
  140.         } /* else */
  141.     } while (Running == TRUE); /* do */
  142. } /* DoIDCMP() */
  143.  
  144. /* ------------------------------------------------------------------------------------------------------------ */
  145.  
  146. void SetupCx()
  147. {
  148.     struct NewBroker Cx_NewBroker = {
  149.         NB_VERSION,
  150.         "LimitWhite",
  151.         "LimitWhite V1.0 - ToneMaster, 2000",
  152.         "Reduces white colours to grey",
  153.         NBU_UNIQUE | NBU_NOTIFY,
  154.         0,
  155.         0,
  156.         NULL,
  157.         0
  158.     };
  159.  
  160.     /* Set up commodities port */
  161.  
  162.     if (!(Cx_Broker_Mp = CreateMsgPort()))
  163.     {
  164.         RemovePatches();
  165.         CleanUp();
  166.     } /* if */
  167.  
  168.     Cx_NewBroker.nb_Port = Cx_Broker_Mp;
  169.  
  170.     if (!(Cx_Broker = (CxObj *)CxBroker(&Cx_NewBroker, NULL)))
  171.     {
  172.         RemovePatches();
  173.         CleanUp();
  174.     } /* if */
  175.  
  176.     ActivateCxObj(Cx_Broker, 1L);
  177. } /* SetupCx() */
  178.  
  179. /* ------------------------------------------------------------------------------------------------------------ */
  180.  
  181. void AttemptExit()
  182. {
  183.     /* Attempt to remove the patches */
  184.  
  185.     if (RemovePatches())
  186.     {
  187.         Running = FALSE;
  188.     } /* if */
  189.  
  190.     else
  191.     {
  192.         DoEasyReq("A program has patched LimitWhite's patches.\nLimitWhite cannot quit, but can be disabled");
  193.     } /* else */
  194. } /* AttemptExit() */
  195.  
  196. /* ------------------------------------------------------------------------------------------------------------ */
  197.  
  198. void CleanUp()
  199. {
  200.     CxMsg *msg;
  201.  
  202.     /* Free system resources */
  203.  
  204.     if (Cx_Broker)
  205.     {
  206.         DeleteCxObjAll(Cx_Broker);
  207.     } /* if */
  208.  
  209.     while (msg = (CxMsg *)GetMsg(Cx_Broker_Mp))
  210.     {
  211.         ReplyMsg((struct Message *)msg);
  212.     } /* while */
  213.  
  214.     DeleteMsgPort(Cx_Broker_Mp);
  215.  
  216.     exit(0);
  217. } /* CleanUp() */
  218.  
  219. /* ------------------------------------------------------------------------------------------------------------ */
  220.  
  221. /* End Of Text */
  222.